package com.font.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.font.bean.Menu;
import com.font.bean.result.Result;
import com.font.mapper.MenuMapper;
import com.font.service.MenuService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * (Menu)表服务实现类
 *
 * @author makejava
 * @since 2023-07-10 14:59:32
 */
@Service("menuService")
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Override
    public Result MenuList() {
        LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Menu::getMenuStatic,0);
        queryWrapper.orderByAsc(Menu::getOrderNum);
        List<Menu> menuAll = this.getBaseMapper().selectList(queryWrapper);
        List<Menu> trees = new ArrayList<>();
        for (Menu menu : menuAll) {
            if (menu.getParentId()==0){
                trees.add(getChren(menuAll,menu));

            }
        }
        Result result = new Result(200,"查询成功");
        result.setData(trees);
        return result;
    }

    private Menu getChren(List<Menu> menuAll,Menu parentMenu) {
        parentMenu.setChrentlist(new ArrayList<Menu>());
        for (Menu menu : menuAll) {
          if (parentMenu.getMenuId()==menu.getParentId()){
              if (parentMenu.getChrentlist()==null) {
                  parentMenu.setChrentlist(new ArrayList<>());
              }
              parentMenu.getChrentlist().add(getChren(menuAll,menu));
          }
        }
        return parentMenu;

    }
}
